//
//  LLLoadingFooterView.m
//  PandaMakeUp
//
//  Created by dl on 16/1/13.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "LLLoadingFooterView.h"

@implementation LLLoadingFooterView {
    UILabel *titleLabel;
    LLUIImageView *loadingImgView;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];

        titleLabel = [UILabel new];
        titleLabel.textColor = UI_COLOR_FONT_LIGHT_GREY;
        titleLabel.font = SUIT_FONT(14);
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.hidden = YES;
        [self addSubview:titleLabel];

        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.centerX.width.equalTo(self);
            make.height.mas_equalTo(SUIT_H(20));
        }];

        loadingImgView = [LLUIImageView new];
        UIImage *image = [UIImage imageNamed:@"pull_down_loading"];
        image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        [loadingImgView setImage:image];
        loadingImgView.tintColor = [UIColor lightGrayColor];
        loadingImgView.hidden = YES;
        [self addSubview:loadingImgView];

        [loadingImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.equalTo(self);
            make.height.width.mas_equalTo(SUIT_H(20));
        }];
    }
    return self;
}

- (void)circleLoadingView {
    if (_state != LLRefreshStateRefreshing) {
        return;
    }
    if ([loadingImgView.layer animationKeys].count == 0) {
        CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        animation.values = @[@(0), @(2 * M_PI)];
        animation.keyTimes = @[@0, @1];
        animation.duration = 0.8f;
        animation.repeatCount = INFINITY;
        [loadingImgView.layer addAnimation:animation forKey:@"loadingRoatation"];
    }

    @weakify(self)
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (0.8f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        @strongify(self)
        [self circleLoadingView];
    });
}

- (void)setState:(LLRefreshState)state {
    if (_state != state) {
        _state = state;
        loadingImgView.hidden = YES;
        titleLabel.hidden = YES;
        [loadingImgView.layer removeAllAnimations];
        if (_state == LLRefreshStateRefreshing) {
            loadingImgView.hidden = NO;
            [self circleLoadingView];
        }
        if (_state == LLRefreshStateNoMoreData) {
            loadingImgView.hidden = YES;
            titleLabel.hidden = NO;
            titleLabel.text = @"已无更多数据";
        } else if (_state == LLRefreshStateIdle || _state == LLRefreshStatePulling || _state == LLRefreshStateWillRefresh) {
            loadingImgView.hidden = YES;
            titleLabel.hidden = NO;
            titleLabel.text = @"上拉获取更多数据";
        }
    }
}

@end
